using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MicroMsg.UI.Page
{
	public class ViewPhoneContact : BasePage
	{
		private const string TAG = "ViewPhoneContact";
		private const int LoadingView = 0;
		private const int InfoView = 0;
		private const int NoContactView = 1;
		private const string ScrollingState = "Scrolling";
		private const string NotScrollingState = "NotScrolling";
		private ScrollViewer mScrollViewer;
		private bool mBIsScrollHandlerSet;
		private bool mBIsLoaded;
		private EventWatcher mWatcherPhoneContact;
		private EventWatcher mWatcherHeadImgAdd;
		private ObservableCollection<MobileContact> mListBoxSource;
		private Dictionary<string, MobileContact> mDictMobiles = new Dictionary<string, MobileContact>();
		private ApplicationBar mBar;
		internal Grid LayoutRoot;
		internal Grid sencondRoot;
		internal panelTitle2Line panelTitle;
		internal Grid ContentPanel;
		internal ViewFlipper ListViewFlipper;
		internal ListBox mListBoxMobileContact;
		internal StackPanel panelNoMobileContact;
		internal Image imgNoContact;
		internal TextBlock txtNoContactNote;
		private bool _contentLoaded;
		public bool IsScrolling
		{
			get;
			private set;
		}
		public ViewPhoneContact()
		{
			this.InitializeComponent();
			PhoneContactListItem.sBIsListScrolling = false;
			base.add_Loaded(new RoutedEventHandler(this.ViewPhoneContact_Loaded));
			base.set_ApplicationBar(this.GetApplicationBar());
		}
		~ViewPhoneContact()
		{
		}
		private void ViewPhoneContact_Loaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("ViewPhoneContact", "setScrollHandler start");
			this.setScrollHandler();
			Log.smethod_5("ViewPhoneContact", "setScrollHandler finish");
			this.initEventWatcher();
			if (!this.mBIsLoaded)
			{
				this.StartLoad();
				this.mBIsLoaded = true;
			}
		}
		private void setScrollHandler()
		{
			if (this.mBIsScrollHandlerSet)
			{
				return;
			}
			this.mBIsScrollHandlerSet = true;
			this.mScrollViewer = (ScrollViewer)UIElementTraveler.FindElementRecursive(this.mListBoxMobileContact, typeof(ScrollViewer));
			FrameworkElement frameworkElement = VisualTreeHelper.GetChild(this.mScrollViewer, 0) as FrameworkElement;
			if (frameworkElement != null)
			{
				VisualStateGroup visualStateGroup = UIElementTraveler.FindVisualState(frameworkElement, "ScrollStates");
				if (visualStateGroup != null)
				{
					visualStateGroup.add_CurrentStateChanging(new EventHandler<VisualStateChangedEventArgs>(this.OnScrollStateChanging));
				}
			}
		}
		private void OnScrollStateChanging(object sender, VisualStateChangedEventArgs e)
		{
			this.IsScrolling = (e.get_NewState().get_Name() == "Scrolling");
			if (e.get_NewState().get_Name() == "Scrolling")
			{
				PhoneContactListItem.sBIsListScrolling = true;
				return;
			}
			if (e.get_NewState().get_Name() == "NotScrolling")
			{
				PhoneContactListItem.sBIsListScrolling = false;
				PhoneContactListItem.RequestHeadImgInView();
				this.mListBoxMobileContact.UpdateLayout();
				if (this.mScrollViewer != null)
				{
					this.mScrollViewer.ScrollToVerticalOffset(this.mScrollViewer.get_VerticalOffset());
				}
			}
		}
		protected override void OnPageIn(object[] object_0)
		{
			Log.smethod_5("ViewPhoneContact", "OnPageIn");
			if (object_0 == null)
			{
			}
		}
		protected override void OnPageOut()
		{
			Log.smethod_5("ViewPhoneContact", "OnPageOut");
			this.unInitEventWatcher();
		}
		private void initEventWatcher()
		{
			if (this.mWatcherPhoneContact == null)
			{
				this.mWatcherPhoneContact = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventPhoneContact));
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_GETFRIEND_SUCCESS, this.mWatcherPhoneContact);
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_GETFRIEND_FAIL, this.mWatcherPhoneContact);
				EventCenter.registerEventWatcher(EventConst.ON_SYNC_CONTACT_COMPLETE, this.mWatcherPhoneContact);
				EventCenter.registerEventWatcher(EventConst.ON_SYNC_CONTACT_FAIL, this.mWatcherPhoneContact);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatcherPhoneContact);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CONTACT_MODIFY, this.mWatcherPhoneContact);
			}
			if (this.mWatcherHeadImgAdd == null)
			{
				this.mWatcherHeadImgAdd = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventHandlerHeadImgAdd));
				EventCenter.registerEventWatcher(EventConst.ON_GET_CURRENT_HEADIMG_COMPLETE, this.mWatcherHeadImgAdd);
			}
		}
		private void unInitEventWatcher()
		{
			if (this.mWatcherPhoneContact != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_GETFRIEND_SUCCESS, this.mWatcherPhoneContact);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_GETFRIEND_FAIL, this.mWatcherPhoneContact);
				EventCenter.removeEventWatcher(EventConst.ON_SYNC_CONTACT_COMPLETE, this.mWatcherPhoneContact);
				EventCenter.removeEventWatcher(EventConst.ON_SYNC_CONTACT_FAIL, this.mWatcherPhoneContact);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatcherPhoneContact);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CONTACT_MODIFY, this.mWatcherPhoneContact);
				this.mWatcherPhoneContact = null;
			}
			if (this.mWatcherHeadImgAdd != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_GET_CURRENT_HEADIMG_COMPLETE, this.mWatcherHeadImgAdd);
				this.mWatcherHeadImgAdd = null;
			}
		}
		private void onEventPhoneContact(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			if (EventConst.ON_NETSCENE_GETFRIEND_SUCCESS == evtArgs.mEventID)
			{
				this.loadMContact();
				this.ShowEmpty();
				return;
			}
			if (EventConst.ON_NETSCENE_GETFRIEND_FAIL == evtArgs.mEventID)
			{
				return;
			}
			if (EventConst.ON_SYNC_CONTACT_COMPLETE == evtArgs.mEventID)
			{
				this.loadMContact();
				this.ShowEmpty();
				return;
			}
			if (EventConst.ON_SYNC_CONTACT_FAIL == evtArgs.mEventID)
			{
				this.loadMContact();
				this.ShowEmpty();
				return;
			}
			if (EventConst.ON_STORAGE_CONTACT_ADD == evtArgs.mEventID || EventConst.ON_STORAGE_CONTACT_MODIFY == evtArgs.mEventID)
			{
				List<Contact> listObject = evtArgs.getListObject<Contact>();
				if (listObject != null)
				{
					using (List<Contact>.Enumerator enumerator = listObject.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Contact current = enumerator.get_Current();
							if (current != null && !ToolFunction.isEmptyString(current.strUsrName) && this.mDictMobiles != null && this.mDictMobiles.ContainsKey(current.strUsrName))
							{
								MobileContact mobileContact = this.mDictMobiles.get_Item(current.strUsrName);
								if (mobileContact != null)
								{
									if (ContactMgr.isContact(current) && 2 != mobileContact.nStatus)
									{
										mobileContact.nStatus = 2;
										PhoneContactListItem.RefreshStatusInView(mobileContact.strUsrName);
									}
									else
									{
										if (!ContactMgr.isContact(current) && 2 == mobileContact.nStatus)
										{
											mobileContact.nStatus = 1;
											PhoneContactListItem.RefreshStatusInView(mobileContact.strUsrName);
										}
									}
								}
							}
						}
					}
				}
			}
		}
		private void onEventHandlerHeadImgAdd(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			if (EventConst.ON_GET_CURRENT_HEADIMG_COMPLETE == evtArgs.mEventID)
			{
				if (evtArgs.mObject != null)
				{
					if (evtArgs.mObject.GetType() == typeof(Dictionary<string, Stream>))
					{
						PhoneContactListItem.RefreshHeadImgInView(evtArgs.mObject as Dictionary<string, Stream>);
						return;
					}
				}
				Log.smethod_5("ViewPhoneContact", "evtArgs Error:onEventHandlerHeadImgAdd:ON_GET_CURRENT_HEADIMG_COMPLETE");
				return;
			}
		}
		private void PhoneContactListItem_ItemClick(object sender, object object_0)
		{
			if (object_0 != null)
			{
				MobileContact mobileCon = object_0 as MobileContact;
				ContactDetailViewForAll.GotoContactDitailView(mobileCon, false);
			}
		}
		private void loadMContact()
		{
			List<MobileContact> list = StorageMgr.mobileContact.getList();
			if (list != null && list.get_Count() != 0)
			{
				list = Enumerable.ToList<MobileContact>(Enumerable.OrderBy<MobileContact, string>(list, (MobileContact mobileContact_0) => mobileContact_0.strRealNameQuanPin));
				this.mListBoxSource = new ObservableCollection<MobileContact>();
				List<MobileContact> list2 = new List<MobileContact>();
				List<MobileContact> list3 = new List<MobileContact>();
				List<MobileContact> list4 = new List<MobileContact>();
				using (List<MobileContact>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						MobileContact current = enumerator.get_Current();
						if (1 != current.nType)
						{
							if (current.strUsrName == null)
							{
								current.strUsrName = "";
							}
							if (ToolFunction.isEmptyString(current.strRealNameQuanPin))
							{
								if (!ToolFunction.isEmptyString(current.strRealName))
								{
									current.strRealNameQuanPin = GetPinYin.ConvertStrToQuanPin(current.strRealName);
								}
								else
								{
									current.strRealNameQuanPin = "";
								}
							}
							if (1 == current.nStatus)
							{
								list2.Add(current);
							}
							else
							{
								if (2 == current.nStatus)
								{
									list3.Add(current);
								}
								else
								{
									list4.Add(current);
								}
							}
						}
					}
				}
				using (List<MobileContact>.Enumerator enumerator2 = list2.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						MobileContact current2 = enumerator2.get_Current();
						this.mListBoxSource.Add(current2);
					}
				}
				using (List<MobileContact>.Enumerator enumerator3 = list4.GetEnumerator())
				{
					while (enumerator3.MoveNext())
					{
						MobileContact current3 = enumerator3.get_Current();
						this.mListBoxSource.Add(current3);
					}
				}
				using (List<MobileContact>.Enumerator enumerator4 = list3.GetEnumerator())
				{
					while (enumerator4.MoveNext())
					{
						MobileContact current4 = enumerator4.get_Current();
						this.mListBoxSource.Add(current4);
					}
				}
				this.mListBoxMobileContact.set_ItemsSource(this.mListBoxSource);
				using (IEnumerator<MobileContact> enumerator5 = this.mListBoxSource.GetEnumerator())
				{
					while (enumerator5.MoveNext())
					{
						MobileContact current5 = enumerator5.get_Current();
						if (!this.mDictMobiles.ContainsKey(current5.strUsrName))
						{
							this.mDictMobiles.Add(current5.strUsrName, current5);
						}
					}
				}
				this.mListBoxMobileContact.UpdateLayout();
				return;
			}
		}
		private void StartLoad()
		{
			this.disableAllControls(null);
			if (StorageMgr.mobileContact.count() > 0)
			{
				this.loadMContact();
				this.ShowEmpty();
			}
			ServiceCenter.mobileContactMgr.syncAndUploadContactFromDevice(new DelegateProgressBar(this.setProgressBar));
		}
		public void setProgressBar(string strTitleProgressBar)
		{
			if (strTitleProgressBar != null)
			{
				this.disableAllControls(strTitleProgressBar);
				return;
			}
			this.enableAllControls();
		}
		private void ShowEmpty()
		{
			this.enableAllControls();
			this.ListViewFlipper.ShowView(0);
			if (this.mListBoxSource == null || this.mListBoxSource.get_Count() == 0)
			{
				this.ListViewFlipper.ShowView(1);
			}
		}
		private void enableAllControls()
		{
			ProcessingCover.releaseProgressBar(this.LayoutRoot);
		}
		private void disableAllControls(string strTitleProgressBar)
		{
			if (strTitleProgressBar == null)
			{
				ProcessingCover.getProgressBar(this.LayoutRoot, strings.PCon_List_MsgReading, null, false, false, null);
				return;
			}
			ProcessingCover.setProgressBarTitle(this.LayoutRoot, strTitleProgressBar, null, false, false, null);
		}
		public IApplicationBar GetApplicationBar()
		{
			if (this.mBar != null)
			{
				return this.mBar;
			}
			this.mBar = new ApplicationBar();
			this.mBar.set_IsMenuEnabled(true);
			this.mBar.set_IsVisible(true);
			this.mBar.set_Opacity(0.7);
			ApplicationBarIconButton applicationBarIconButton = new ApplicationBarIconButton(new Uri("/Resource/Img/CommonUse/icon_search.png", 2));
			applicationBarIconButton.set_Text(strings.COM_Search);
			applicationBarIconButton.add_Click(new EventHandler(this.searchButton_Click));
			this.mBar.get_Buttons().Add(applicationBarIconButton);
			return this.mBar;
		}
		private void searchButton_Click(object sender, EventArgs e)
		{
			List<SearchItem> list = new List<SearchItem>();
			int type = 7;
			if (this.mListBoxSource != null)
			{
				using (IEnumerator<MobileContact> enumerator = this.mListBoxSource.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						MobileContact current = enumerator.get_Current();
						if (current.strRealName != null)
						{
							list.Add(new SearchItem(current, current.strRealName.Trim(), type, null));
						}
					}
				}
			}
			SearchAlgorithm searchAlgorithm = new SearchAlgorithm(list);
			ToolFunction.GotoPageForUserControl("/Source/UI/ListSearchHelper/ListSearchPage.xaml", new object[]
			{
				searchAlgorithm
			}, false);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/PhoneContact/ViewPhoneContact.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.sencondRoot = (Grid)base.FindName("sencondRoot");
			this.panelTitle = (panelTitle2Line)base.FindName("panelTitle");
			this.ContentPanel = (Grid)base.FindName("ContentPanel");
			this.ListViewFlipper = (ViewFlipper)base.FindName("ListViewFlipper");
			this.mListBoxMobileContact = (ListBox)base.FindName("mListBoxMobileContact");
			this.panelNoMobileContact = (StackPanel)base.FindName("panelNoMobileContact");
			this.imgNoContact = (Image)base.FindName("imgNoContact");
			this.txtNoContactNote = (TextBlock)base.FindName("txtNoContactNote");
		}
	}
}
